# from django.db import models
#
#
# # Create your models here.
#
# class Resource(models.Model):
#     name = models.CharField(max_length=50, verbose_name="资源名")
#     parent = models.ForeignKey(to="self", to_field="id", on_delete=models.CASCADE, verbose_name="父级资源", null=True,
#                                blank=True, related_name='son')
#     url = models.CharField(max_length=50, verbose_name="资源路径", null=True, blank=True)
#     promtion = models.CharField(max_length=50, verbose_name="权限值", null=True, blank=True)
#
#     def __str__(self):
#         return self.name
#
#     class Meta:
#         db_table = 'resource'
#
#
# class Role(models.Model):
#     name = models.CharField(max_length=50, verbose_name="角色名")
#     resources = models.ManyToManyField(to="Resource", verbose_name="拥有的资源", related_name='role')
#     promtion = models.CharField(max_length=50, verbose_name="权限值", null=True, blank=True)
#
#     def __str__(self):
#         return self.name
#
#     class Meta:
#         db_table = 'role'
#
#
# class User(models.Model):
#     username = models.CharField(max_length=50, verbose_name="用户名")
#     password = models.CharField(max_length=50, verbose_name="密码")
#     phone = models.CharField(max_length=11, verbose_name="手机号")
#     rid = models.ForeignKey(to="Role", to_field="id", on_delete=models.CASCADE, verbose_name="角色")
#
#     def __str__(self):
#         return self.username
#
#     class Meta:
#         db_table = 'user'
#
#
# class ResourceInterface(models.Model):
#     resid = models.ForeignKey(Resource, on_delete=models.SET_NULL, null=True, related_name='interface')
#     url = models.CharField(max_length=100)
#
#     class Meta:
#         verbose_name = '接口权限表'
#         verbose_name_plural = '接口权限表'
#         db_table = "resource_interface"
from django.db import models


class Resource(models.Model):
    name = models.CharField(max_length=50, verbose_name="资源名")
    parent = models.ForeignKey(to="self", to_field="id", on_delete=models.CASCADE, verbose_name="父级资源", null=True,
                             blank=True, related_name='son')
    url = models.CharField(max_length=50, verbose_name="资源路径", null=True, blank=True)
    # 使用位掩码存储权限
    promtion = models.BigIntegerField(default=0, verbose_name="权限值")

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'resource'


class Role(models.Model):
    name = models.CharField(max_length=50, verbose_name="角色名")
    # 使用位掩码存储权限
    promtion = models.BigIntegerField(default=0, verbose_name="权限值")

    class Meta:
        db_table = 'role'


class User(models.Model):
    username = models.CharField(max_length=50, verbose_name="用户名")
    password = models.CharField(max_length=50, verbose_name="密码")
    phone = models.CharField(max_length=11, verbose_name="手机号")
    rid = models.ForeignKey(to="Role", to_field="id", on_delete=models.CASCADE, verbose_name="角色")

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'user'


class ResourceInterface(models.Model):
    resid = models.ForeignKey(Resource, on_delete=models.SET_NULL, null=True, related_name='interface')
    url = models.CharField(max_length=100)

    class Meta:
        verbose_name = '接口权限表'
        verbose_name_plural = '接口权限表'
        db_table = "resource_interface"


